#include <stdio.h>
#include <stdbool.h>

#define NUM_PROCESSES 5
#define NUM_RESOURCES 3

int available[NUM_RESOURCES];
int maximum[NUM_PROCESSES][NUM_RESOURCES];
int allocation[NUM_PROCESSES][NUM_RESOURCES];
int need[NUM_PROCESSES][NUM_RESOURCES];

void initializeSystem() {
    // 初始化可用资源
    available[0] = 10; available[1] = 5; available[2] = 7;

    // 初始化最大需求
    maximum[0][0] = 7; maximum[0][1] = 5; maximum[0][2] = 3;
    maximum[1][0] = 3; maximum[1][1] = 2; maximum[1][2] = 2;
    maximum[2][0] = 9; maximum[2][1] = 0; maximum[2][2] = 2;
    maximum[3][0] = 2; maximum[3][1] = 2; maximum[3][2] = 2;
    maximum[4][0] = 4; maximum[4][1] = 3; maximum[4][2] = 3;

    // 初始化已分配资源
    allocation[0][0] = 0; allocation[0][1] = 1; allocation[0][2] = 0;
    allocation[1][0] = 2; allocation[1][1] = 0; allocation[1][2] = 0;
    allocation[2][0] = 3; allocation[2][1] = 0; allocation[2][2] = 2;
    allocation[3][0] = 2; allocation[3][1] = 1; allocation[3][2] = 1;
    allocation[4][0] = 0; allocation[4][1] = 0; allocation[4][2] = 2;

    // 初始化剩余需求
    for (int i = 0; i < NUM_PROCESSES; i++) {
        for (int j = 0; j < NUM_RESOURCES; j++) {
            need[i][j] = maximum[i][j] - allocation[i][j];
        }
    }
}

bool isSafeState() {
    int work[NUM_RESOURCES];
    bool finish[NUM_PROCESSES] = {false};

    for (int i = 0; i < NUM_RESOURCES; i++) {
        work[i] = available[i];
    }

    while (true) {
        bool found = false;
        for (int i = 0; i < NUM_PROCESSES; i++) {
            if (!finish[i]) {
                bool canAllocate = true;
                for (int j = 0; j < NUM_RESOURCES; j++) {
                    if (need[i][j] > work[j]) {
                        canAllocate = false;
                        break;
                    }
                }
                if (canAllocate) {
                    for (int j = 0; j < NUM_RESOURCES; j++) {
                        work[j] += allocation[i][j];
                    }
                    finish[i] = true;
                    found = true;
                }
            }
        }
        if (!found) {
            for (int i = 0; i < NUM_PROCESSES; i++) {
                if (!finish[i]) {
                    return false;
                }
            }
            return true;
        }
    }
}

int requestResources(int process, int request[]) {
    for (int i = 0; i < NUM_RESOURCES; i++) {
        if (request[i] > need[process][i]) {
            return -1; // 请求超过最大需求
        }
        if (request[i] > available[i]) {
            return -2; // 请求超过可用资源
        }
    }

    for (int i = 0; i < NUM_RESOURCES; i++) {
        available[i] -= request[i];
        allocation[process][i] += request[i];
        need[process][i] -= request[i];
    }

    if (!isSafeState()) {
        for (int i = 0; i < NUM_RESOURCES; i++) {
            available[i] += request[i];
            allocation[process][i] -= request[i];
            need[process][i] += request[i];
        }
        return -3; // 系统进入不安全状态
    }

    return 0; // 请求成功
}

void releaseResources(int process, int release[]) {
    for (int i = 0; i < NUM_RESOURCES; i++) {
        available[i] += release[i];
        allocation[process][i] -= release[i];
        need[process][i] += release[i];
    }
}

int main() {
    initializeSystem();

    int process = 1;
    int request1[NUM_RESOURCES] = {1, 0, 2};
    int request2[NUM_RESOURCES] = {3, 2, 0};
    int release[NUM_RESOURCES] = {0, 1, 0};

    int result = requestResources(process, request1);
    if (result == 0) {
        printf("资源请求成功\n");
    } else {
        printf("资源请求失败，错误代码：%d\n", result);
    }

    releaseResources(process, release);
    printf("资源释放成功\n");

    result = requestResources(process, request2);
    if (result == 0) {
        printf("资源请求成功\n");
    } else {
        printf("资源请求失败，错误代码：%d\n", result);
    }

    return 0;
}
